﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrinterObject
    {
        public static Printer[] GetPrinters()
        {
            return Query(null);
        }

        public static Printer GetPrinter(int id)
        {
            Printer printer = new Printer();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable factories = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printer.Id                = Convert.ToInt32(factories.Rows[0][PrinterDesc.Id]);
            printer.Name              = Convert.ToString(factories.Rows[0][PrinterDesc.Name]);
            printer.PageWidth         = Convert.ToInt32(factories.Rows[0][PrinterDesc.PageWidth]);
            printer.TopMargin         = Convert.ToInt32(factories.Rows[0][PrinterDesc.TopMargin]);
            printer.bottomMargin      = Convert.ToInt32(factories.Rows[0][PrinterDesc.bottomMargin]);
            printer.LeftMargin        = Convert.ToInt32(factories.Rows[0][PrinterDesc.LeftMargin]);
            printer.RightMargin       = Convert.ToInt32(factories.Rows[0][PrinterDesc.RightMargin]);

            return printer;
        }

        public static Printer GetPrinter(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterDesc.Name, ConditionExpress.EQUALS, name);

            Printer[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Printer[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrinterDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                Printer[] printers = new Printer[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Printer printer = new Printer();

                    printer.Id               = Convert.ToInt32(table.Rows[i][PrinterDesc.Id]);
                    printer.Name             = Convert.ToString(table.Rows[i][PrinterDesc.Name]);
                    printer.PageWidth        = Convert.ToInt32(table.Rows[i][PrinterDesc.PageWidth]);
                    printer.TopMargin        = Convert.ToInt32(table.Rows[i][PrinterDesc.TopMargin]);
                    printer.bottomMargin     = Convert.ToInt32(table.Rows[i][PrinterDesc.bottomMargin]);
                    printer.LeftMargin       = Convert.ToInt32(table.Rows[i][PrinterDesc.LeftMargin]);
                    printer.RightMargin      = Convert.ToInt32(table.Rows[i][PrinterDesc.RightMargin]);


                    printers.SetValue(printer, i);
                }

                return printers;
            }
            return null;
        }

        public static Int32 Add(Printer printer)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrinterDesc.Name,             printer.Name);
            builder.InsertAdd(PrinterDesc.PageWidth,        printer.PageWidth);
            builder.InsertAdd(PrinterDesc.TopMargin,        printer.TopMargin);
            builder.InsertAdd(PrinterDesc.bottomMargin,     printer.bottomMargin);
            builder.InsertAdd(PrinterDesc.LeftMargin,       printer.LeftMargin);
            builder.InsertAdd(PrinterDesc.RightMargin,      printer.RightMargin);

            builder.QueryTableName = PrinterDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Printer printer)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrinterDesc.Name,              printer.Name);
            builder.UpdateAdd(PrinterDesc.PageWidth,         printer.PageWidth);
            builder.UpdateAdd(PrinterDesc.TopMargin,         printer.TopMargin);
            builder.UpdateAdd(PrinterDesc.bottomMargin,      printer.bottomMargin);
            builder.UpdateAdd(PrinterDesc.LeftMargin,        printer.LeftMargin);
            builder.UpdateAdd(PrinterDesc.RightMargin,       printer.RightMargin);

            builder.ConditionAdd(PrinterDesc.Id, ConditionExpress.EQUALS, printer.Id);
            builder.QueryTableName = PrinterDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

    }
}
